﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using liaowa2.Impl.Manager;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace liaowa2.Dao.Web
{
  public  class SmsLogDao
    {
        public int Add(SmsLogModel model)
        {
            StringBuilder strSql = new StringBuilder();
            using (IDbConnection conn = SqlConn.OpenConnectionStar())
            {
                var trans = conn.BeginTransaction();

                strSql.Append("insert into SmsLog(");
                strSql.Append("Phone,Code,ReturnMessage,CreateTime,source");
                strSql.Append(") values (");
                strSql.Append("@Phone,@Code,@ReturnMessage,@CreateTime,@source");
                strSql.Append(") ");
                strSql.Append(";select @@IDENTITY");

                var p = new DynamicParameters();
                p.Add("@Phone", model.Phone);
                p.Add("@Code", model.Code);
                p.Add("@ReturnMessage", model.ReturnMessage);
                p.Add("@CreateTime", model.CreateTime);
                p.Add("@source", model.source);
                var r = conn.Query<int>(strSql.ToString(), p, trans, commandType: CommandType.Text).FirstOrDefault();
                if (r > 0)
                {
                    return r;
                }
                else
                {
                    return 0;
                }
            }

        }
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="Phone">手机号码</param>
        /// <param name="SmsTemplateCode">模板编号</param>

        /// <param name="source">来源</param>
        public void sendmsg(string Phone, string SmsTemplateCode, string source)
        {
            string strnumber = GetNumber();
            SmsLogModel models = new SmsLogModel();
            models = GetphoneModel(Phone);
            ITopClient client = new DefaultTopClient("http://gw.api.taobao.com/router/rest", "24417848", "1027efbb36589e3f7e66402eb6161351");
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.Extend = "";
            req.SmsType = "normal";
            req.SmsFreeSignName = "撩娃育儿";
            req.SmsParam = "{name:'" + strnumber + "'}";
            req.RecNum = Phone;
            req.SmsTemplateCode = SmsTemplateCode;
            AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);
            var body = rsp.Body;
            if (models != null)
            {
                //if (Delete(models.id))
                //{

                //}
                models.Code = Convert.ToInt32(strnumber);
                models.CreateTime = DateTime.Now;
                models.Phone = Phone;
                models.source = source;
                models.ReturnMessage = body;
                Add(models);
            }
            else
            {
                SmsLogModel model = new SmsLogModel();
                model.Code = Convert.ToInt32(strnumber);
                model.CreateTime = DateTime.Now;
                model.Phone = Phone;
                model.source = source;
                model.ReturnMessage = body;
                Add(model);
            }




        }

        public SmsLogModel GetphoneModel(string phone)
        {
            var p = new DynamicParameters();
            p.Add("@phone", phone);

            using (IDbConnection conn = SqlConn.OpenConnectionStar())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select top 1 * ");
                strSql.Append(" FROM SmsLogModel ");
                strSql.Append(" where Phone=@Phone order by CreateTime desc");

                var model = conn.Query<SmsLogModel>(strSql.ToString(), p, commandType: CommandType.Text).FirstOrDefault();
                return model;
            }
        }
     
        private static string GetNumber()
        {
            ArrayList MyArray = new ArrayList();
            Random random = new Random();
            string str = null;

            //循环的次数     
            int Nums = 6;
            while (Nums > 0)
            {
                int i = random.Next(1, 9);
                //  if (!MyArray.Contains(i))  
                //  {  
                if (MyArray.Count < 6)
                {
                    MyArray.Add(i);
                }
                // }  
                Nums -= 1;
            }
            for (int j = 0; j <= MyArray.Count - 1; j++)
            {
                str += MyArray[j].ToString();
            }
            return str;
        }
    }
}
